﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Web.Admin.ashx
{
    /// <summary>
    /// filemanage 的摘要说明
    /// </summary>
    public class filemanage : Evan.WebUI.ManagerAshxHelper
    {
        private List<Evan.Model.SysModel.FileModel> Files = new List<Evan.Model.SysModel.FileModel>();
        Evan.Common.FileManager.Files FileManager = new Evan.Common.FileManager.Files();
        public override void ProcessRequest(HttpContext context)
        {
            string action = Evan.Common.CommFun.GetQueryString("action");
            if (!IsAdminLogin())
            {
                JsonResult(context, -99, "管理员未登陆");
            }
            else
            {
                switch (action)
                {
                    case "getlist":
                        GetFileList(context);
                        break;
                    case "createfolder":
                        CreateFolder(context);
                        break;
                    case "delete":
                        Delete(context);
                        break;
                    case "copy":
                        Copy(context);
                        break;
                    case "cut":
                        Cut(context);
                        break;
                    case "rename":
                        ReName(context);
                        break;
                    default:
                        break;
                }
            }
        }
        private void ReName(HttpContext context)
        {
            try
            {
                string FileListStr = GetParam("filelist");
                Evan.Model.SysModel.FileModel FileModel = Evan.Common.JsonHelper.JSONToObject<Evan.Model.SysModel.FileModel>(FileListStr);

                if (FileModel.Type == "File")
                {
                    //重命名文件  
                    Evan.Common.DirFile.ReNameFile(new System.IO.FileInfo(context.Server.MapPath(FileModel.FullPath)), FileModel.Name);
                }
                else
                {
                    //重命名目录
                    Evan.Common.DirFile.ReNameDir(new System.IO.DirectoryInfo(context.Server.MapPath(FileModel.FullPath)), FileModel.Name);
                }

                JsonResult(context, 1, "重命名成功");
            }
            catch (System.Threading.ThreadAbortException) { throw; }
            catch (Exception ex)
            {
                Evan.SystemLog.MemLog.AddExLog2("文件管理 - 重命名操作 (重命名失败)", null, ex.ToString());
                JsonResult(context, 0, "重命名失败");
            }
        }
        private void Cut(HttpContext context)
        {
            try
            {
                string FileListStr = GetParam("filelist");
                List<Evan.Model.SysModel.FileModel> FileList = Evan.Common.JsonHelper.JsonToList<Evan.Model.SysModel.FileModel>(FileListStr);
                string FileManagerPath = Evan.Common.CommFun.GetAppSetting("FileManagerPath");
                foreach (var item in FileList)
                {
                    item.CurPath = string.IsNullOrEmpty(FileManagerPath) ? "" : "/" + FileManagerPath + item.CurPath + item.Name;
                    if (item.Type == "File")
                    {
                        //移动文件  
                        new System.IO.FileInfo(context.Server.MapPath(item.FullPath)).MoveTo(context.Server.MapPath(item.CurPath));
                    }
                    else
                    {
                        //移动目录
                        new System.IO.DirectoryInfo(context.Server.MapPath(item.FullPath)).MoveTo(context.Server.MapPath(item.CurPath));
                    }
                }
                JsonResult(context, 1, "移动成功");
            }
            catch (System.Threading.ThreadAbortException) { throw; }
            catch (Exception ex)
            {
                Evan.SystemLog.MemLog.AddExLog2("文件管理 - 移动操作 (移动失败)", null, ex.ToString());
                JsonResult(context, 0, "移动失败");
            }
        }
        
        private void Copy(HttpContext context)
        {
            try
            {
                string FileListStr = GetParam("filelist");
                List<Evan.Model.SysModel.FileModel> FileList = Evan.Common.JsonHelper.JsonToList<Evan.Model.SysModel.FileModel>(FileListStr);
                string FileManagerPath = Evan.Common.CommFun.GetAppSetting("FileManagerPath");
                foreach (var item in FileList)
                {
                    item.CurPath = string.IsNullOrEmpty(FileManagerPath) ? "" : "/" + FileManagerPath + item.CurPath + item.Name;
                    if (item.Type == "File")
                    {
                        //复制文件  
                        Evan.Common.DirFile.CopyFile(item.FullPath,item.CurPath,true);
                    }
                    else
                    {
                        //复制目录
                        Evan.Common.DirFile.CopyDir(item.FullPath,item.CurPath);
                    }
                }
                JsonResult(context, 1, "复制成功");
            }
            catch (System.Threading.ThreadAbortException) { throw; }
            catch (Exception ex)
            {
                Evan.SystemLog.MemLog.AddExLog2("文件管理 - 复制操作 (复制失败)", null, ex.ToString());
                JsonResult(context, 0, "复制失败");
            }
        }

        private void Delete(HttpContext context)
        {
            try
            {
                string FileListStr= GetParam("filelist");
                List<Evan.Model.SysModel.FileModel> FileList = Evan.Common.JsonHelper.JsonToList<Evan.Model.SysModel.FileModel>(FileListStr);

                foreach (var item in FileList)
                {
                    if (item.Type == "File")
                    {
                        //删除文件
                        Evan.Common.DirFile.DeleteFile(item.FullPath);
                    }
                    else
                    {
                        //删除目录
                        Evan.Common.DirFile.DeleteDir(item.FullPath);
                    }
                }
                JsonResult(context, 1, "删除成功");
            }
            catch (System.Threading.ThreadAbortException) { throw; }
            catch (Exception ex)
            {
                Evan.SystemLog.MemLog.AddExLog2("文件管理 - 删除操作 (删除失败)", null, ex.ToString());
                JsonResult(context, 0, "删除失败");
            }
        }
        
        private void CreateFolder(HttpContext context)
        {
            string Dir = GetParam("dir");
            string NewDir = GetParam("newdir");
            bool isok = FileManager.CreateFolder(Dir, NewDir);
            if (isok)
            {
                JsonResult(context, 1, "创建文件夹成功");
            }
            else
            {
                JsonResult(context, 0, "创建文件夹失败");
            }
        }

        private void GetFileList(HttpContext context)
        {
            string Dir = GetParam("dir");
            Files = FileManager.GetAllByPath(Dir, "", true);
            string retval = Evan.Common.JsonHelper.ObjectToJSON2(Files);
            JsonResult2(context, 1, retval);
        }

        
    }
}